using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Kinect_Dance.Screens;

namespace Kinect_Dance
{
    enum GameScreens
    {
        MainMenuScreen,
        AboutScreen,
        ChooseSongScreen,
        GamePlayScreen
    }

    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class MainGame : Microsoft.Xna.Framework.Game
    {
        public static GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;


        MainMenuScreen mMainMenuScreen;
        AboutScreen mAboutScreen;
        ChooseSongScreen mChooseSongScreen;
        GameplayScreen mGamePlayScreen;
        GameScreens mCurrentScreen;


        public MainGame()
        {
            graphics = new GraphicsDeviceManager(this);
            graphics.PreferredBackBufferWidth = 800;
            graphics.PreferredBackBufferHeight = 600;
            Content.RootDirectory = "Content";
            this.IsMouseVisible = true;
            TargetElapsedTime = TimeSpan.FromTicks(333333);
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            // TODO: use this.Content to load your game content here
            mMainMenuScreen = new MainMenuScreen(this);
            mCurrentScreen = GameScreens.MainMenuScreen;
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here
            switch (mCurrentScreen)
            {
                case GameScreens.MainMenuScreen:
                    if (mMainMenuScreen != null)
                    {
                        mMainMenuScreen.gameUpdate(gameTime);
                    }
                    break;
                case GameScreens.AboutScreen:
                    if (mAboutScreen != null)
                    {
                        mAboutScreen.gameUpdate(gameTime);
                    }
                    break;
                case GameScreens.ChooseSongScreen:
                    if (mChooseSongScreen != null)
                    {
                        mChooseSongScreen.gameUpdate(gameTime);
                    }
                    break;
                case GameScreens.GamePlayScreen:
                    if (mGamePlayScreen != null)
                    {
                        mGamePlayScreen.gameUpdate(gameTime);
                    }
                    break;
                default:
                    break;
            }
            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Transparent);
            // TODO: Add your drawing code here
            switch (mCurrentScreen)
            {
                case GameScreens.MainMenuScreen:
                    if (mMainMenuScreen != null)
                    {
                        mMainMenuScreen.gameDraw(gameTime, spriteBatch);
                    }
                    break;
                case GameScreens.AboutScreen:
                    if (mAboutScreen != null)
                    {
                        mAboutScreen.gameDraw(gameTime, spriteBatch);
                    }
                    break;
                case GameScreens.ChooseSongScreen:
                    if (mChooseSongScreen != null)
                    {
                        mChooseSongScreen.gameDraw(gameTime, spriteBatch);
                    }
                    break;
                case GameScreens.GamePlayScreen:
                    if (mGamePlayScreen != null)
                    {
                        mGamePlayScreen.gameDraw(gameTime, spriteBatch);
                    }
                    break;
                default:
                    break;
            }
            base.Draw(gameTime);
        }

        public void startGame()
        {

            release();
            mGamePlayScreen = new GameplayScreen(this);
            mCurrentScreen = GameScreens.GamePlayScreen;
        }

        public void chooseSong()
        {
            release();
            mChooseSongScreen = new ChooseSongScreen(this);
            mCurrentScreen = GameScreens.ChooseSongScreen;
        }

        public void showAuthours()
        {
            release();
            mAboutScreen = new AboutScreen(this);
            mCurrentScreen = GameScreens.AboutScreen;
        }

        private void release()
        {
            mAboutScreen = null;
            mMainMenuScreen = null;
            mGamePlayScreen = null;
            mChooseSongScreen = null;
        }
    }
}
